package com.eastidea.live.servlet;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eastidea.live.util.Constant;

public class SysListener extends HttpServlet implements ServletContextListener {

	private static final long serialVersionUID = 0xe5a9f33cc18dab6L;
	private static final Logger logger = LoggerFactory
			.getLogger(SysListener.class);

	public void contextInitialized(ServletContextEvent sce) {
		logger.info("ContextInitialized START...");
		System.setProperty("java.awt.headless", "true");
		System.setProperty("jmagick.systemclassloader", "no");
		String rootpath = sce.getServletContext().getRealPath("/");
		if (rootpath != null)
			rootpath = rootpath.replaceAll("\\\\", "/");
		else
			rootpath = "/";
		if (!rootpath.endsWith("/"))
			rootpath = (new StringBuilder()).append(rootpath).append("/")
					.toString();
		Constant.SYSTEM_ROOT_PATH = rootpath;
		Constant.CONTENT_PATH = sce.getServletContext().getContextPath();

		logger.info("ContextInitialized END...");

		try {
			InputStream inputStream = this.getClass().getClassLoader()
					.getResourceAsStream("appConfig.properties");
			Properties p = new Properties();
			p.load(inputStream);
			Constant.WEB_ROOT = p.getProperty("web.root");
			Constant.CREATION_ROOT = Constant.WEB_ROOT
					+ p.getProperty("creation.path");
			Constant.DISPLAY_ROOT = Constant.WEB_ROOT
					+ p.getProperty("display.path");

//			Class.forName("org.postgresql.Driver");
//
//			Connection conn = DriverManager.getConnection(
//					"jdbc:postgresql:palmio", "postgres", "postgres");
//			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt
//					.executeQuery("SELECT name, id from sys_weather_city");
//			while (rs.next()) {
//				Constant.WEATHER_CITY_CODE.put(rs.getString(1),
//						String.valueOf(rs.getLong(2)));
//			}
//
//			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void contextDestroyed(ServletContextEvent servletcontextevent) {
	}

}